package cn.tedu.baking.controller;

import cn.tedu.baking.mapper.UserMapper;
import cn.tedu.baking.pojo.dto.UserLoginDTO;
import cn.tedu.baking.pojo.dto.UserRegDTO;
import cn.tedu.baking.pojo.dto.UserUpdateDTO;
import cn.tedu.baking.pojo.entity.User;
import cn.tedu.baking.pojo.ov.UserVO;
import cn.tedu.baking.response.JsonResult;
import cn.tedu.baking.response.StatusCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;

@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    UserMapper userMapper;

    @PostMapping("/reg")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO) {
        UserVO userVO = userMapper.selectByUserName(userRegDTO.getUserName());
        if (userVO != null) {
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        User user = new User();
        user.setUserName(userRegDTO.getUserName());
        user.setNickName(userRegDTO.getNickName());
        user.setPassword(userRegDTO.getPassword());
        user.setCreateTime(new Date());
        userMapper.insert(user);
        return JsonResult.ok();
    }

    @RequestMapping("/login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, HttpSession session) {
        UserVO userVO = userMapper.selectByUserName(userLoginDTO.getUserName());
        System.out.println("userLoginDTO::" + userLoginDTO.toString());
        System.out.println("userVO::" + userVO.toString());
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }

        if (userVO.getPassword().equals(userLoginDTO.getPassword())) {
            //把当前登录的用户对象保存到会话对象中
            session.setAttribute("user", userVO);
            return JsonResult.ok();
        } else {

            return new JsonResult(StatusCode.PASSWORD_ERROR);
        }
    }

    @GetMapping("/logout")
    public JsonResult logout(HttpSession session) {
        session.removeAttribute("user");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    public JsonResult update(@RequestBody UserUpdateDTO userUpdateDTO, HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        //判断出是否需要更换头像
        if (userUpdateDTO.getImgUrl() != null) {
        //得到原图片路径
            String imgUrl = userMapper.selectImgUrlById(userUpdateDTO.getId());
    //删除对应的图片文件
            new File("E:\\Code\\ideaCode\\file" + imgUrl).delete();
        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        user.setId(userVO.getId());
        userMapper.update(user);
        return JsonResult.ok();
    }

    @GetMapping("")
    public JsonResult list() {
        return JsonResult.ok(userMapper.select());
    }

    @PostMapping("{id}/{isAdmin}/change")
    public JsonResult change(@PathVariable Long id,
                             @PathVariable Integer isAdmin) {
        User user = new User();
        user.setId(id);
        user.setIsAdmin(isAdmin);
        userMapper.update(user);
        return JsonResult.ok();
    }

    @PostMapping("{id}/delete")
    public JsonResult delete(@PathVariable Long id) {
        userMapper.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("/show")
    public JsonResult show(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }

    @GetMapping("/showUrl")
    public JsonResult showUrl(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.FORBIDDEN_ERROR);
        }else {
            String imgUrl = userVO.getImgUrl();
            return JsonResult.ok(imgUrl);
        }
    }
}
